package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.common.utils.SoftHideKeyBoardUtil;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.adapter.PhoneListAdapter;
import com.e2oto.seller.modules.seller.adapter.SelectImageAdapter;
import com.e2oto.seller.modules.seller.bean.SellerInfoBean;
import com.e2oto.seller.modules.seller.presenter.ShopInfoPresenter;
import com.e2oto.seller.modules.seller.views.ShopInfoView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;

/**
 * Created by ZhangLiQiang on 2017/11/10.
 */

public class ShopInfoActivity extends BaseMvpActivity<ShopInfoView, ShopInfoPresenter> implements ShopInfoView {

    @BindView(R.id.mine_seller_album_gv)
    GridView albumGridView;
    @BindView(R.id.shop_phone_ll)
    LinearLayout phoneLinearLayout;
    @BindView(R.id.shop_info_logo_iv)
    ImageView shopLogoIv;
    @BindView(R.id.shop_info_name)
    TextView shopNameEt;
    @BindView(R.id.shop_info_address)
    TextView shopAddressTv;
    @BindView(R.id.shop_info_main_business_tv)
    TextView mainBusinessTv;
    @BindView(R.id.shop_info_region)
    TextView shopRegionTv;


    private SelectImageAdapter selectImageAdapter;
    private PhoneListAdapter phoneListAdapter;
    private String shopLogoPath;//店铺logo

    @Override
    public ShopInfoPresenter createPresenter() {
        return new ShopInfoPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_shop_info;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        SoftHideKeyBoardUtil.assistActivity(this);
        phoneListAdapter = new PhoneListAdapter(this, new ArrayList<String>(), phoneLinearLayout);
        selectImageAdapter = new SelectImageAdapter(this, 4);
        albumGridView.setAdapter(selectImageAdapter);
        mPresenter.getSellerInfo(SellerApp.sellerId);
    }

    @OnClick({R.id.toolbar_left_btn, R.id.shop_info_commit, R.id.mine_add_phone, R.id.shop_info_logo_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.shop_info_commit:
                String shopName = shopNameEt.getText().toString().trim();
                String shopAddress = shopAddressTv.getText().toString().trim();
                if (TextUtils.isEmpty(shopLogoPath)) {
                    showMessage("请选择您的店铺logo");
                    return;
                }
                if (TextUtils.isEmpty(shopName)) {
                    showMessage("请输入店铺名称");
                    return;
                }
                if (TextUtils.isEmpty(shopAddress)) {
                    showMessage("请选择您店铺所在详细地址");
                    //return;
                }
                if (phoneListAdapter.getList() == null || phoneListAdapter.getList().size() == 0) {
                    showMessage("至少添加一个联系电话");
                    return;
                }
                if (!phoneListAdapter.checkPhoneValid()) {
                    return;
                }
                if (selectImageAdapter.getStringList() == null || selectImageAdapter.getStringList().size() == 0) {
                    showMessage("请上传店铺详情图片");
                    return;
                }
                mPresenter.commitSellerInfo(SellerApp.sellerId,"9068/12054/12055/12101","王所乡/花园村/花园村3组",
                        shopAddress,phoneListAdapter.getList(), selectImageAdapter.getStringList(), shopLogoPath);
                break;
            case R.id.mine_add_phone:
                if (phoneListAdapter.getList().size() >= 5) {
                    showMessage("超过最大限制");
                    return;
                }
                phoneListAdapter.getList().add("");
                phoneListAdapter.notifyDataSetChanged();
                break;
            case R.id.shop_info_logo_iv:
                PhotoPicker.builder().setPhotoCount(1).setShowCamera(true).setShowGif(true).setPreviewEnabled(false).start(this, 6686);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                List<String> images = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                for (String imagePath : images) {
                    selectImageAdapter.addOneImage(imagePath);
                }
            }
        } else if (resultCode == RESULT_OK && requestCode == 6686) {
            List<String> images = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            shopLogoPath = images.get(0);
            ImageLoader.load(this, shopLogoPath, shopLogoIv);
        }
    }


    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void getSellerInfoCallback(SellerInfoBean sellerInfoBean) {
        shopLogoPath = sellerInfoBean.getLogo();
        phoneListAdapter.getList().addAll(sellerInfoBean.getMobile_string());
        phoneListAdapter.notifyDataSetChanged();
        selectImageAdapter.addStringList(sellerInfoBean.getCover_string());
        ImageLoader.load(this, shopLogoPath, shopLogoIv);
        shopNameEt.setText(sellerInfoBean.getSeller_name());
        shopRegionTv.setText(sellerInfoBean.getArea_tree_name_show());
        shopAddressTv.setText(sellerInfoBean.getTown_tree_name_show() + " " + sellerInfoBean.getDetail_address());
        mainBusinessTv.setText(sellerInfoBean.getCategory_name());
    }

    @Override
    public void commitSellerInfoSuccessCallback(String msg) {
        showMessage(msg);
        finish();
    }
}
